package time.code.added;

import java.time.LocalDate;
import java.time.MonthDay;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

/**
 * Description: JDK1.8新增的时间操作api示例
 *
 * @author code-worker
 * @date 2021/08/21 20:58
 */
public class LocalDateDemo {

    /**
     * task: 获取当前日期及详情
     * @date 2021/8/21 22:17
     */
    public void getCurrentDateAndDetails(){
        LocalDate today = LocalDate.now();
        System.out.println("当前时间: "+today);
        System.out.println("当前日期详情-年份: "+today.getYear());
        System.out.println("当前日期详情-月份: "+today.getMonth());
        System.out.println("当前日期详情-天数: "+today.getDayOfYear());
        System.out.println("当前日期详情-日期: "+today.getDayOfMonth());
        System.out.println("当前日期详情-周期: "+today.getDayOfWeek()+"\n");
    }

    /**
     * task: 自定义日期
     * @date 2021/8/21 22:28
     */
    public void handleSpecialDate(){
        LocalDate lastYearBirthday = LocalDate.parse("2020-01-29");
        System.out.println("去年生日: "+ lastYearBirthday);
        LocalDate birthday = LocalDate.of(2021,1,29);
        System.out.println("今年生日: "+birthday);
        LocalDate nextYearBirthday = LocalDate.ofYearDay(2022, 29);
        System.out.println("明年生日: "+nextYearBirthday+"\n");
    }

    /**
     * task: 格式转换日期
     * @date 2021/8/22 14:38
     */
    public void parseDateFormat(){
        LocalDate today = LocalDate.parse("2021-08-22");
        System.out.println("日期: "+ today);
        String todayFormat = "20210822";
        LocalDate parseToday = LocalDate.parse(todayFormat, DateTimeFormatter.BASIC_ISO_DATE);
        System.out.println("日期(通过格式转换): "+parseToday);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
        String parseTodayFormat = today.format(formatter);
        System.out.println("日期(自定义格式): "+parseTodayFormat);
    }

    /**
     * task: 时间比较
     * @date 2021/8/21 22:34
     */
    public void compareDate(){
        LocalDate today = LocalDate.now();
        LocalDate customDate = LocalDate.of(2021, 8, 21);
        System.out.println("\n时间比较: "+today+"和"+customDate+
                (today.equals(customDate)? "是":"不是")+"同一天\n");
    }

    /**
     * task: 获取月日(可以用于获取每年中周期性节日，如生日、法定假日等)
     * @date 2021/8/21 22:57
     */
    public void getMonthDay(){
        LocalDate todayWithYear = LocalDate.now();
        // 直接获取今天月日：MonthDay.now()
        MonthDay monthDay = MonthDay.of(todayWithYear.getMonth(), todayWithYear.getDayOfMonth());
        System.out.println("今天的月日: "+ monthDay);
    }

    /**
     * task: 获取年月(可以用于期货期权到期日等
     *       此外，YearMonth 实例的 lengthOfMonth() 方法可以返回当月的天数，在判断2月有28天还是29天时非常有用
     * @date 2021/8/22 13:54
     */
    public void getYearMonth(){
        LocalDate todayWithDay = LocalDate.now();
        // 直接获取今天年月: YearMonth.now()
        YearMonth yearMonth = YearMonth.of(todayWithDay.getYear(), todayWithDay.getMonth());
        System.out.println("今天的年月: "+ yearMonth);
        YearMonth yearMonth2 = YearMonth.of(todayWithDay.getYear(), 2);
        System.out.println("今年2月的天数: "+yearMonth2.lengthOfMonth());
    }

    /**
     * task: 判断今年是否是闰年
     * @date 2021/8/22 14:00
     */
    public void isLeapYear(){
        LocalDate today = LocalDate.now();
        if(today.isLeapYear()){
            System.out.println("今年是闰年");
        }else {
            System.out.println("今年是平年");
        }
    }

    /**
     * task: 计算一天|一周|一月|一年|十年|百年|千年前或后的日期
     * @date 2021/8/22 10:39
     */
    public void plusOrMinusDate(){
        LocalDate today = LocalDate.now();
        LocalDate yesterday = today.minus(1, ChronoUnit.DAYS);
        LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS);
        LocalDate nextWeek = today.plus(1, ChronoUnit.WEEKS);
        LocalDate nextMonth = today.plus(1, ChronoUnit.MONTHS);
        LocalDate nextYear = today.plus(1, ChronoUnit.YEARS);
        LocalDate next10Year = today.plus(1, ChronoUnit.DECADES);
        LocalDate next100Year = today.plus(1, ChronoUnit.CENTURIES);
        LocalDate next1000Year = today.plus(1, ChronoUnit.MILLENNIA);
        System.out.println("\n当前时间: " + today);
        System.out.println("昨天: " + yesterday);
        System.out.println("明天: " + tomorrow);
        System.out.println("下周的当前时间: "+nextWeek);
        System.out.println("下个月的当前时间: "+nextMonth);
        System.out.println("明年的当前时间: "+nextYear);
        System.out.println("十年后的当前时间: "+next10Year);
        System.out.println("百年后的当前时间: "+next100Year);
        System.out.println("千年后的当前时间: "+next1000Year);
    }

    /**
     * task: 判断时间先后
     * @date 2021/8/22 10:45
     */
    public void checkTimeBeforeAndAfter(){
        LocalDate today = LocalDate.now();
        LocalDate yesterday = LocalDate.now().minusDays(1);
        LocalDate tomorrow = LocalDate.now().plusDays(1);
        if(yesterday.isBefore(today)){
            System.out.println("\n"+yesterday+" 早于今天");
        }
        if(tomorrow.isAfter(today)){
            System.out.println(tomorrow+" 晚于今天");
        }
    }

    public static void main(String[] args) {
        LocalDateDemo demo = new LocalDateDemo();
        demo.getCurrentDateAndDetails();
        demo.handleSpecialDate();
        demo.parseDateFormat();
        demo.compareDate();
        demo.getMonthDay();
        demo.getYearMonth();
        demo.isLeapYear();
        demo.plusOrMinusDate();
        demo.checkTimeBeforeAndAfter();
    }

}
